package com.examplemartin.jpademo.service;


import com.examplemartin.jpademo.dao.UserDao;
import com.examplemartin.jpademo.pojo.po.UserPO;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
@Component
public class UserService {

  @Resource
  private UserDao userDao;

  /**
   * 添加用户
   */
  public UserPO insertUser(UserPO userPO){
    UserPO save = userDao.save(userPO);
    return save;
  }

  /**
   * 返回单条用户记录
   * @param id
   * @return UserPO
   */
  public UserPO findUser(Long id){
    UserPO userPO = userDao.findById(id).get();
    return userPO;
  }

  /**
   * 返回所有用户记录
   * @return all--List<UserPO>
   */
  public List<UserPO> findUser(){
    List<UserPO> all = userDao.findAll();
    return all;
  }




  public UserPO login(String username,String password){

    List<UserPO> byLoginnameAndPassword = userDao.findByLoginnameAndPassword(username, password);
    if(byLoginnameAndPassword.size()>1||byLoginnameAndPassword.size()<=0){
      return null;
    }
    return byLoginnameAndPassword.get(0);
  }

  // 通过用户 登入名找出用户 -- 验证是否 有重复
  public Integer countUserByLoginName(String loginName){
    return userDao.countByLoginname(loginName);
  }

  // 新建用户
  public UserPO insertUserPO(UserPO userPO){
    return  userDao.save(userPO);
  }

  // 按用户id查找用户
  public UserPO findUserById(Long id){
    UserPO userPO = userDao.findById(id).get();
    return userPO;
  }


}
